import { createRouter, createWebHashHistory } from 'vue-router'
import { Notify } from 'vant'
import store from '../store'
const home = () => import('views/home/Home')
const category = () => import('views/category/Category')
const shopcart = () => import('views/shopcart/Shopcart')
const profile = () => import('views/profile/Profile')
const detail = () => import('views/detail/Detail')
const register = () => import('views/profile/Register')
const login = () => import('views/profile/Login')
const createOrder = () => import('views/createorder/CreateOrder')
const address = () => import('views/profile/Address.vue')
const editAddress = () => import('views/profile/EditAddress.vue')
const orderDetail = () => import('views/orderDetail/OrderDetail')
const order = () => import('views/order/Order')
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
    meta: {
      title: '图书兄弟'
    }
  },
  {
    path: '/category',
    component: category,
    meta: {
      title: '图书兄弟-商品分类'
    }
  },
  {
    path: '/shopcart',
    component: shopcart,
    meta: {
      title: '图书兄弟-购物车',
      // 判断是否需要授权
      isAuthRequired: true
    }
  },
  {
    path: '/profile',
    component: profile,
    meta: {
      title: '图书兄弟-个人中心',
      // 判断是否需要授权
      isAuthRequired: true
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: detail,
    meta: {
      title: '图书兄弟-详情页'
    },
    props: true
  },
  {
    path: '/register',
    name: 'register',
    component: register,
    meta: {
      title: '图书兄弟-注册'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: login,
    meta: {
      title: '图书兄弟-登录'
    }
  },
  {
    path: '/createorder',
    name: 'createorder',
    component: createOrder,
    meta: {
      title: '图书兄弟-订单'
    }
  },
  {
    path: '/address',
    name: 'address',
    component: address,
    meta: {
      title: '图书兄弟-地址管理',
      isAuthRequired: true
    }
  },
  {
    path: '/editaddress',
    name: 'editaddress',
    component: editAddress,
    meta: {
      title: '图书兄弟-地址编辑',
      isAuthRequired: true
    }
  },
  {
    path: '/orderdetail',
    name: 'orderdetail',
    component: orderDetail,
    meta: {
      title: '图书兄弟-订单详情',
      isAuthRequired: true
    }
  },
  {
    path: '/order',
    name: 'order',
    component: order,
    meta: {
      title: '图书兄弟-订单列表',
      isAuthRequired: true
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAuthRequired && !store.state.login.isLogin) {
    Notify('您还没有登录，请先登录')
    return next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  }
  next()
  document.title = to.meta.title
})
export default router
